package cn.edu.hunau.java.week03.day04;

import org.junit.Test;

import java.io.*;
import java.nio.charset.Charset;

public class WriteTest {

    @Test
    public void testWrite(){
        String data="2025年是抗日胜利80周年";
        try(FileOutputStream fos=new FileOutputStream("dir/hsl.text");){
            fos.write(data.getBytes());
            fos.flush();
        }catch(IOException e){
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testRed() throws IOException{
        try(FileInputStream fis=new FileInputStream("dir/hsl.text");){
            byte[] buff=new byte[1024];
            int count=fis.read(buff);
            String content=new String(buff,0,count);
            System.out.println(content);
        }
    }

    @Test
    public void testWrite2() throws IOException{
        try(FileOutputStream fos=new FileOutputStream("dir/hsl.text");
            OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
            BufferedWriter bw=new BufferedWriter(osw);){
            bw.write("hello world");
            bw.newLine();
            bw.write("暑假快结束了...");
            bw.newLine();
            bw.flush();
        }
    }

    @Test
    public void testRead(){
        try(FileInputStream fis=new FileInputStream("dir/hsl.text");
        InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
        BufferedReader br=new BufferedReader(isr);
        ){
            String data=null;
            while((data=br.readLine())!=null){
                System.out.println(data);
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    @Test
    public void testfw() throws Exception{
        try(FileWriter fw=new FileWriter("dir/hsl.text", Charset.forName("UTF-8"),true);
        ){
            fw.write("/n");
            fw.write("FileWriter写入的内容");
            fw.write("/n");
            fw.flush();
        }
    }

    @Test
    public void testPw(){

    }
}
